package com.mq.listener;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import sun.util.resources.LocaleData;

import java.time.LocalTime;
import java.util.Locale;
import java.util.Map;

@Component
public class RabbitMQListener {
    //BasicQueue
    @RabbitListener(queues = "simple.queue")
    public void RabbitListenerSimpleQueue(String msg) {
        System.out.println("消费者接收到==>" + msg);
    }

    /**
     * WorkQueue
     * 消费者速度不同，默认均分(消息预取机制)，不满足能者多劳。
     * 需配置prefetch
     */
    @RabbitListener(queues = "work.queue")
    public void RabbitListenerWorkQueue1(String msg) throws InterruptedException {
        System.out.println("消费者1接收到simple.queue【" + msg + "】" + LocalTime.now());
        Thread.sleep(20);
    }

    @RabbitListener(queues = "work.queue")
    public void RabbitListenerWorkQueue2(String msg) throws InterruptedException {
        System.err.println("消费者2接收到simple.queue==>【" + msg + "】" + LocalTime.now());
        Thread.sleep(200);
    }
    //fanout 广播
    @RabbitListener(queues = "fanout.queue1")
    public void RabbitListenerFanoutQueue1(String msg) {
        System.out.println("消费者1接收到fanout.queue1==>【" + msg + "】");
    }

    @RabbitListener(queues = "fanout.queue2")
    public void RabbitListenerFanoutQueue2(String msg) {
        System.err.println("消费者2接收到fanout.queue2==>【" + msg + "】");
    }

//    @RabbitListener(bindings = @QueueBinding(
//            value = @Queue(name = "fanout.queue2"),
//            exchange = @Exchange(name = "exchange.fanout", type = ExchangeTypes.FANOUT)))
//    public void RabbitListenerFanoutQueue3(String msg) {
//        System.err.println("消费者3接收到fanout.queue2==>【" + msg + "】");
//    }

    //direct 定向
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue1"),
            exchange = @Exchange(name = "exchange.direct", type = ExchangeTypes.DIRECT),
            key = {"a", "b"}))
    public void RabbitListenerDirectQueue1(String msg) {
        System.out.println("消费者接收到direct.queue1==>【" + msg + "】");
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue2"),
            exchange = @Exchange(name = "exchange.direct", type = ExchangeTypes.DIRECT),
            key = {"a", "c"}))
    public void RabbitListenerDirectQueue2(String msg) {
        System.out.println("消费者接收到direct.queue2==>【" + msg + "】");
    }

    /**
     * topic 通配符
     * # 匹配一个或多个
     * * 匹配一个
     * @param msg
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.queue1"),
            exchange = @Exchange(name = "exchange.topic",type = ExchangeTypes.TOPIC),
            key = "#.news"))
    public void RabbitListenerTopicQueue1(String msg){
        System.out.println("消费者接收到topic.queue1==>【" + msg + "】");
    }
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.queue2"),
            exchange = @Exchange(name = "exchange.topic",type = ExchangeTypes.TOPIC),
            key = "china.#"))
    public void RabbitListenerTopicQueue2(String msg){
        System.out.println("消费者接收到topic.queue2==>【" + msg + "】");
    }

    /**
     *
     * @param msg 与发送方消息类型一致
     */
    @RabbitListener(queues = "object.queue")
    public void RabbitListenerObjectQueue(Map<String,Object> msg) {
        System.out.println("消费者接收到==>" + msg);
    }
}
